/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package manager;

import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import model.Packet;
import model.PacketGenerator;

/**
 *
 * @author wpc
 */
public class PacketGeneratorManager {

    private PriorityQueue<PacketGenerator> queue;

    public PacketGeneratorManager() {
        queue = new PriorityQueue<PacketGenerator>();
    }

    public void add(PacketGenerator g){
        queue.add(g);
    }

    public List<Packet> next(){
        PacketGenerator minG = queue.poll();
        while(minG!=null&&minG.isEmpty())
            minG = queue.poll();

        if(null!=minG){
            List<Packet> packetList = new LinkedList<Packet>();
            packetList.add(minG.next());
            if(!minG.isEmpty())
              queue.add(minG);
            return packetList;
        }else{
            return null;
        }
    }

    public List<Packet> next(double endTime){
        List<Packet> resultList = new LinkedList<Packet>();
        PacketGenerator pg = queue.peek();
        while(pg!=null&&pg.now()<endTime){
            pg = queue.poll();
            resultList.addAll(pg.next(endTime));
            if(!pg.isEmpty())
              queue.add(pg);
            pg = queue.peek();
        }
        return resultList;
    }

}
